package com.jd.basic.util;


import com.jd.basic.jwt.*;
import com.jd.user.domain.Logininfo;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

public class LoginContext {


    public static UserInfo getUserInfo(HttpServletRequest request){
        String token = request.getHeader("token");
        // 获取公钥
        PublicKey publicKey = RsaUtils.getPublicKey(JwtUtils.class.getClassLoader().getResource(JwtRasHolder.INSTANCE.getJwtRsaPubData()).getFile());
        // 通过token获取载荷
        Payload<UserInfo> payload = JwtUtils.getInfoFromToken(token, publicKey, UserInfo.class);
        if (null == payload || null == payload.getUserInfo()){
            return null;
        }
        return payload.getUserInfo();
    }


    public static UserInfo getUserInfo(){
        return ThreadLocalUtil.get();
    }

    public static Logininfo getLogininfo(){
        return ThreadLocalUtil.get().getLogininfo();
    }
}
